﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using KCL.Code;
using KCL.Code.Business;

namespace KCL.Management
{
    public partial class Material : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[SessionManager.InSection] != null)
            {
                if ((int)Session[SessionManager.InSection] == 1)
                {
                    btnAddMaterial.Enabled = dgvMaterial.Columns[9].Visible = (bool)Session[SessionManager.canCreate];
                    imgbtnAddProduct.Visible = imgbtnAddColor.Visible = imgbtnAddSize.Visible =
                    imgbtnAddThickness.Visible = imgbtnAddMadeIn.Visible = (bool)Session[SessionManager.canCreate];
                    btnUpdateMaterial.Enabled = (bool)Session[SessionManager.canModify];
                    lblNextMaterialCode.Text = MaterialServices.GetNextMaterialCode();
                    return;
                }
            }
            Response.Redirect("~/Default.aspx");
        }

        protected void btnAddMaterial_Click(object sender, EventArgs e)
        {
            Code.Material m = new Code.Material();
            m.code = lblNextMaterialCode.Text.Trim().ToUpper();
            if (m.code == string.Empty)
            {
                lblMessageMaterial.ForeColor = System.Drawing.Color.Red;
                lblMessageMaterial.Text = string.Format("Thêm nguyên liệu thất bại vì mã bằng rỗng.", m.code);
                return;
            }

            m.name = txtMaterialName.Text.Trim();
            m.productId = int.Parse(cboProduct.SelectedValue);
            m.colorId = int.Parse(cboColor.SelectedValue);
            m.sizeId = int.Parse(cboSize.SelectedValue);
            m.thicknessId = int.Parse(cboThickness.SelectedValue);
            m.madeInId = int.Parse(cboMadeIn.SelectedValue);
            m.quantity = 0;
            m.isActive = true;
            if (MaterialServices.Insert(m))
            {
                dgvMaterial.DataBind();
                lblMessageMaterial.ForeColor = System.Drawing.Color.Green;
                lblMessageMaterial.Text = string.Format("Thêm nguyên liệu '{0}' thành công.", m.code);
            }
            else
            {
                lblMessageMaterial.ForeColor = System.Drawing.Color.Red;
                lblMessageMaterial.Text = string.Format("Thêm nguyên liệu '{0}' thất bại.", m.code);
            }
            lblNextMaterialCode.Text = MaterialServices.GetNextMaterialCode();
        }

        protected void dgvMaterial_SelectedIndexChanged(object sender, EventArgs e)
        {
            string code = (string)dgvMaterial.SelectedDataKey.Value;
            Code.Material m = MaterialServices.GetMaterialByID(code);
            try
            {
                lblMaterialCode.Text = m.code;
                txtMaterialName.Text = m.name;
                cboProduct.SelectedValue = m.productId.ToString();
                cboColor.SelectedValue = m.colorId.ToString();
                cboSize.SelectedValue = m.sizeId.ToString();
                cboThickness.SelectedValue = m.thicknessId.ToString();
                cboMadeIn.SelectedValue = m.madeInId.ToString();
            }
            catch (Exception) { }
        }

        protected void btnUpdateMaterial_Click(object sender, EventArgs e)
        {
            Code.Material m = new Code.Material();
            m.code = lblMaterialCode.Text.Trim().ToUpper();
            m.name = txtMaterialName.Text.Trim();
            m.productId = int.Parse(cboProduct.SelectedValue);
            m.colorId = int.Parse(cboColor.SelectedValue);
            m.sizeId = int.Parse(cboSize.SelectedValue);
            m.thicknessId = int.Parse(cboThickness.SelectedValue);
            m.madeInId = int.Parse(cboMadeIn.SelectedValue);
            if (MaterialServices.Update(m))
            {
                dgvMaterial.DataBind();
                lblMessageMaterial.ForeColor = System.Drawing.Color.Green;
                lblMessageMaterial.Text = string.Format("Cập nhật nguyên liệu '{0}' thành công.", m.code);
            }
            else
            {
                lblMessageMaterial.ForeColor = System.Drawing.Color.Red;
                lblMessageMaterial.Text = string.Format("Cập nhật nguyên liệu '{0}' thất bại.", m.code);
            }
        }

        protected void btnAddColor_Click(object sender, EventArgs e)
        {
            if (txtColorName.Text.Trim().Length > 0)
            {
                Color c = new Color();
                c.name = txtColorName.Text.Trim();
                c.isActive = true;
                if (ColorServices.Insert(c))
                {
                    txtColorName.Text = string.Empty;
                    lblMessageColor.Text = string.Format("Thêm '{0}' thành công.", c.name);
                    cboColor.DataBind();
                }
                else
                {
                    lblMessageColor.Text = string.Format("Thêm '{0}' thất bại.", c.name);
                }
            }
            ModalPopupExtender1.Show();
        }

        protected void btnAddSize_Click(object sender, EventArgs e)
        {
            if (txtLength.Text.Trim().Length > 0 &&
                txtWidth.Text.Trim().Length > 0)
            {
                Size s = new Size();
                s.length = txtLength.Text.Trim();
                s.width = txtWidth.Text.Trim();
                s.isActive = true;
                if (SizeServices.Insert(s))
                {
                    txtLength.Text = txtWidth.Text = string.Empty;
                    lblMessageSize.Text = string.Format("Thêm '{0} x {1}' thành công.", s.length, s.width);
                    cboSize.DataBind();
                }
                else
                {
                    lblMessageSize.Text = string.Format("Thêm '{0} x {1}' thất bại.", s.length, s.width);
                }
            }
            ModalPopupExtender2.Show();
        }

        protected void btnAddThickness_Click(object sender, EventArgs e)
        {
            if (txtThicknessName.Text.Trim().Length > 0)
            {
                Thickness t = new Thickness();
                t.thick = txtThicknessName.Text.Trim();
                t.isActive = true;
                if (ThicknessServices.Insert(t))
                {
                    txtThicknessName.Text = string.Empty;
                    lblMessageThick.Text = string.Format("Thêm '{0}' thành công.", t.thick);
                    cboThickness.DataBind();
                }
                else
                {
                    lblMessageThick.Text = string.Format("Thêm '{0}' thất bại.", t.thick);
                }
            }
            ModalPopupExtender3.Show();
        }

        protected void btnAddMadeIn_Click(object sender, EventArgs e)
        {
            if (txtMadeIn.Text.Trim().Length > 0)
            {
                MadeIn m = new MadeIn();
                m.name = txtMadeIn.Text.Trim();
                m.isActive = true;
                if (MakeServices.Insert(m))
                {
                    txtMadeIn.Text = string.Empty;
                    lblMessageMadeIn.Text = string.Format("Thêm '{0}' thành công.", m.name);
                    cboMadeIn.DataBind();
                }
                else
                {
                    lblMessageMadeIn.Text = string.Format("Thêm '{0}' thất bại.", m.name);
                }
            }
            ModalPopupExtender4.Show();
        }

        protected void btnAddProduct_Click(object sender, EventArgs e)
        {
            if (txtProductName.Text.Trim().Length > 0)
            {
                Product p = new Product();
                p.code = ProductServices.GetNextProductCode();
                p.name = txtProductName.Text.Trim();
                p.isActive = true;
                if (ProductServices.Insert(p))
                {
                    lblMessageProduct.Text = string.Format("Thêm '{0}' mã {1} thành công.", p.name, p.code);
                    cboProduct.DataBind();
                }
                else
                {
                    lblMessageProduct.Text = string.Format("Thêm '{0}' thất bại.", p.name);
                }
            }
            ModalPopupExtender5.Show();
        }
    }
}